﻿/*------------------------------------------------------------------------------------------
Component    : Dicom Communication
Source file   : Form1.cs
Author    : Hai Nguyen  
Creation Date  : 06/02/2012
Description   : This class is contain event of dicom sever.
Use Case    :this class used in Dicom Communication use case
Includes    : Utilities, Network, Dicom
Notes  : <Optional/additional comments regarding the source file> -       
Optional 
Copyright Prowess, Inc. 2011 
 
The copyright to the computer program(s) herein is the property 
of Prowess, Inc. The program(s) may be used and/or copied only 
with the written permission of Prowess, Inc. or in accordance 
with the terms and conditions stipulated in the agreement/contract 
under which the program(s) have been supplied.
 --------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClearCanvas.Dicom.Network;
using System.Net.Sockets;
using System.Net;
using ClearCanvas.Dicom;
using ClearCanvas.Utilities;

namespace Prowess.Lynx.DicomCommunicator
{
    public partial class Form1 : Form
    {
        #region Properties
        delegate void UpdateLogDelegate(string text);
        private ServerAssociationParameters para; 
        #endregion

        #region Constructor
        public Form1()
        {
            InitializeComponent();
        } 
        #endregion

        #region Event
        private void button1_Click(object sender, EventArgs e)
        {
            bool flag = CheckInput();
            if (flag == true)
            {
                IPEndPoint endpoint = new IPEndPoint(IPAddress.Any,int.Parse(textBox2.Text));
                para = new ServerAssociationParameters(textBox_AE.Text, endpoint);
                byte pcid = para.AddPresentationContext(SopClass.VerificationSopClass);
               // para.AddPresentationContext(SopClass.)
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                para.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);

                pcid = para.AddPresentationContext(SopClass.CtImageStorage);

                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                para.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);

                pcid = para.AddPresentationContext(SopClass.RtDoseStorage);

                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                para.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);

                pcid = para.AddPresentationContext(SopClass.RtStructureSetStorage);

                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                para.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);

                pcid = para.AddPresentationContext(SopClass.RtPlanStorage);

                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                para.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                para.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
                para.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);

                pcid = para.AddPresentationContext(SopClass.StudyRootQueryRetrieveInformationModelFind);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);

                pcid = para.AddPresentationContext(SopClass.PatientRootQueryRetrieveInformationModelFind);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);

                pcid = para.AddPresentationContext(SopClass.PatientStudyOnlyQueryRetrieveInformationModelFindRetired);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);

                pcid = para.AddPresentationContext(SopClass.PatientRootQueryRetrieveInformationModelMove);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);

                pcid = para.AddPresentationContext(SopClass.StudyRootQueryRetrieveInformationModelMove);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);

                pcid = para.AddPresentationContext(SopClass.PatientStudyOnlyQueryRetrieveInformationModelMoveRetired);
                para.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
                para.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrBigEndian);
                try
                {
                    if (DicomServer.StartListening(para, StartServer)==true)
                    {
                        richTextBox_Log.Text += "\n Dicom Server is started Listening\n";
                        
                        button1.Enabled = false;
                        button2.Enabled = true;
                    }
                    else
                    {
                        richTextBox_Log.Text += "\n Dicom Server is Not started Listening\n";
                    }


                }

                catch (System.Exception ex)
                {
                    richTextBox_Log.Text += "\n " + ex.Message + "\n";
                }
            }

        }
        /// <summary>
        /// Check data input
        /// </summary>
        /// <returns></returns>
        private bool CheckInput()
        {
            if (textBox_AE.Text == "" || textBox_AE.Text.Length > 16)
            {
                MessageBox.Show("Error input Textbox AE.Please try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox_AE.Focus();
                return false;
            }
            else return true;
        }
        /// <summary>
        /// Start server function
        /// </summary>
        /// <param name="server"></param>
        /// <param name="assoc"></param>
        /// <returns></returns>
        private IDicomServerHandler StartServer(DicomServer server, ServerAssociationParameters assoc)
        {
            return new DicomServerHandler(server, assoc, textBox_foldername.Text, this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Stop listen
            if (para != null)
            {
                try
                {
                    DicomServer.StopListening(para);
                    richTextBox_Log.Text += "\n Dicom Server is stopped Listen\n";
                    button1.Enabled = true;
                    button2.Enabled = false;
                }
                catch (System.Exception ex)
                {
                    richTextBox_Log.Text += "\n " + ex.Message + "\n";
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderSelected = new FolderBrowserDialog();
            if (folderSelected.ShowDialog() == DialogResult.OK)
            {
                textBox_foldername.Text = folderSelected.SelectedPath;
            }
        }
        /// <summary>
        /// Set Log
        /// </summary>
        /// <param name="log"></param>
        public void SetLog(String log)
        {
            UpdateLog(log);
        }
        /// <summary>
        /// Update Log
        /// </summary>
        /// <param name="text"></param>
        private void UpdateLog(string text)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateLogDelegate(this.UpdateLog), new object[] { text });
            }
            else
            {
                richTextBox_Log.Text += text + "\n";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox_Log.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (para != null)
            {
                try
                {
                    DicomServer.StopListening(para);
                    para = null;
                }
                catch (System.Exception ex)
                {
                    this.Close();
                }
            }

            this.Close();
        } 
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
